package com.mesSystem.service.impl.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mesSystem.dto.ProcessWorkstationResponse;
import com.mesSystem.entity.ProcessWorkstation;
import com.mesSystem.entity.Workstation;
import com.mesSystem.entity.WorkstationAndEmployee;
import com.mesSystem.entity.WorkstationType;
import com.mesSystem.mapper.ProcessWorkstationMapper;
import com.mesSystem.service.impl.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
public class ProcessWorkstationServiceImpl extends ServiceImpl<ProcessWorkstationMapper, ProcessWorkstation> implements ProcessWorkstationService {

    /**
     * 更新工位信息，通过标记在前端标记工位状态，完成工位信息的增删
     *
     * @param workstationIds
     * @param processName
     */
    public void update(List<Long> workstationIds, Long processName) {

    }

    @Autowired
    WorkstationTypeService workstationTypeService;

    /**
     * 根据工序id查询：该工序中使用的工位类型
     *
     * @param processId
     * @return
     */
    @Override
    public List<WorkstationType> queryByProcessId(Integer page, Integer size, Long processId) {
        // 根据工序id，从process_workstation表中取出 工位id集合
        // 根据工位id集合 从workstation_type表中取出 工位类型集合
        QueryWrapper<ProcessWorkstation> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq("process_id", processId);
        List<ProcessWorkstation> processWorkstations = list(queryWrapper);
        log.info(processWorkstations.toString());
        // 从工位id集合中取出工位类型集合
        if (!processWorkstations.isEmpty()) {
            List<Long> workstationTypeIds = processWorkstations.stream()
                    .map(ProcessWorkstation::getWorkstationTypeId)
                    .collect(Collectors.toList());

            QueryWrapper<WorkstationType> workstationTypeWrapper = new QueryWrapper<>();
            workstationTypeWrapper.in("workstation_type_id", workstationTypeIds);
            List<WorkstationType> workstationTypeList = workstationTypeService.list(workstationTypeWrapper);
            log.info(workstationTypeList.toString());
            // 这里你可以根据需要返回工位类型集合，或者进行其他业务逻辑处理
            return workstationTypeList;
        }
        return null;
    }

    /**
     * 根据从产品定义id中，取得工位类型对应的物料类型
     */

    /**
     * 根据工序id查询：该工序中使用的工位类型及数量
     * TODO---改进代码！！！（图省事就堆了屎山🙈）
     *
     * @param processId
     * @return ProcessWorkstationResponse
     */
    @Override
    public List<ProcessWorkstationResponse> queryWorkstationByProcessId(Long processId) {

        // 根据工序id，从process_workstation表中取出 工位id集合
        // 根据工位id集合 从workstation_type表中取出 工位类型集合
        QueryWrapper<ProcessWorkstation> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq("process_id", processId);
        List<ProcessWorkstation> processWorkstations = list(queryWrapper);
        log.info(processWorkstations.toString());
        // 从工位id集合中取出工位类型集合
        if (!processWorkstations.isEmpty()) {
            List<Long> workstationTypeIds = processWorkstations.stream()
                    .map(ProcessWorkstation::getWorkstationTypeId)
                    .collect(Collectors.toList());

            QueryWrapper<WorkstationType> workstationTypeWrapper = new QueryWrapper<>();
            workstationTypeWrapper.in("workstation_type_id", workstationTypeIds);
            List<WorkstationType> workstationTypeList = workstationTypeService.list(workstationTypeWrapper);

            log.info(workstationTypeList.toString());

            // 创建一个ProcessWorkstationResponse List，并将workstationTypeList和数量信息存入。
            List<ProcessWorkstationResponse> responseList = new ArrayList<>();
            for (WorkstationType workstationType : workstationTypeList) {
                ProcessWorkstationResponse response = new ProcessWorkstationResponse();
                response.setWorkstationType(workstationType);
                // 从工序-工位类型表中获取工位类型数量

                // 创建查询条件，同时符合工序id和工位类型id
                QueryWrapper<ProcessWorkstation> processWorkstationQueryWrapper = new QueryWrapper<>();
                processWorkstationQueryWrapper.eq("process_id",processId)
                        .eq("workstation_type_id",workstationType.getWorkstationTypeId());

                Integer quantity = getOne(processWorkstationQueryWrapper)
                        .getQuantity();
                response.setQuantity(quantity);
                responseList.add(response);
            }
            log.info(responseList.toString());
            return responseList;
        }
        return null;
    }
}
